module.exports = angular.module 'libs.directives.angular-validation-support', []
.directive 'angularValidationSupport', ($log, $compile)->
  $log.log 'angular validation support directive init'
  return {
    priority: -1
    restrict: 'A'
    require: 'ngModel'
    scope:
      support: '@'

    compile: (element, attributes)->
      element.removeAttr 'angular-validation-support'
      pre: (scope, elem, attrs, ctrl) ->
        return if !attrs.validator or !scope.support
        defaultMsg =
          minlength: error: "请输入至少 {0}个 字符"
          maxlength: error: "最多只允许输入 {0}个 字符"
        validators = attrs.validator.split ','
        supports = scope.support.split ','
        for support in supports
          for validator in validators
            validator = validator.trim()
            if validator.split('=')[0] is support
              $(elem).attr "#{support}-error-message", _.replace defaultMsg["#{support}"].error, '{0}', validator.split('=')[1]
        $compile(elem)(scope)
        return
  }
